package com.tanhua.server.controller;

import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private CommentsService commentsService;

    /**
     * 发布动态
     *  参数：
     *      String textContent
     *      文件：imageContent[]
     */
    @PostMapping
    public ResponseEntity movements(String textContent,
                                    MultipartFile [] imageContent) throws IOException {
        movementsService.publishMovement(textContent,imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询我的动态
     *   请求地址：/all
     *   请求参数：
     *      page,pagesize,userId
     *   返回值：
     *      ResponseEntity<PageResult>
     */
    @GetMapping("/all")
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10")  Integer pagesize,
                              Long userId) {
        PageResult result = movementsService.findByUserId(page,pagesize,userId);
        return ResponseEntity.ok(result);
    }


    //查询好友动态
    @GetMapping
    public ResponseEntity findFriendMovements(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10")  Integer pagesize) {
        PageResult result = movementsService.findFriendMovements(page,pagesize);
        return ResponseEntity.ok(result);
    }

    //查询推荐动态 GET/movements/recommend
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10")  Integer pagesize) {
        PageResult result = movementsService.findRecommendMovements(page,pagesize);
        return ResponseEntity.ok(result);
    }

    //根据动态id，查询动态详情
    //@GetMapping("/page/pagesize")   /user/4/10

    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String id) {
        MovementsVo vo = movementsService.findMovementById(id);
        return ResponseEntity.ok(vo);
    }

    /**
     * 动态点赞
     *  GET   /:id/like
     *  返回值：
     *      点赞总数
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer count = commentsService.like(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) {
        Integer count = commentsService.dislike(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 谁看过我
     *  请求地址：GET /movements/visitors
     *  返回值：
     *     List<VisitorsVo>
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        List<VisitorsVo> list = movementsService.visitors();
        return ResponseEntity.ok(list);
    }


}
